package tcpClient

import (
	awesomeTask "awesomeTask/commom"
	"awesomeTask/system/log"
	"fmt"
	"net"
	"strconv"
)

const OnStart string = "on_tcp_start"
const OnMessage string = "on_tcp_message"
const OnConnect string = "on_tcp_connect"
const OnDisConnect string = "on_tcp_disconnect"
const OnError string = "on_tcp_err"
const OnClose string = "on_tcp_close"

type TcpClient struct {
	eventMap  map[string][]interface{}
	debug     bool
	ipAddress string
	port      int
	con       net.Conn
}

func (client *TcpClient) Connect(ipAddress string, port int, debug bool) {
	initClient(client)
	client.debug = debug
	/**
	执行绑定的client start函数
	*/
	if _, ok := client.eventMap[OnStart]; ok {
		for _, fun := range client.eventMap[OnStart] {
			go fun.(func())()
		}
		if debug {
			fmt.Println("开启调试模式:", "执行连接前的预处理")
		}
	}
	con, err := net.Dial("tcp", ipAddress+":"+strconv.Itoa(port))
	if err != nil {
		if _, ok := client.eventMap[OnError]; ok {
			for _, errFun := range client.eventMap[OnStart] {
				go errFun.((func(err2 error)))(err)
			}
		}
		if debug {
			log.GetLogger().Error(err.Error())
		}
		return
	}
	if _, ok := client.eventMap[OnConnect]; ok {
		for _, fun := range client.eventMap[OnConnect] {
			go fun.((func(conn net.Conn)))(con)
		}
	}
	client.con = con
	go client.handler(con)
}
func (client *TcpClient) OnStart(fun func()) {
	initClient(client)
	client.eventMap[OnStart] = append(client.eventMap[OnStart], fun)
}
func (client *TcpClient) OnMessage(fun func(connection net.Conn, message string)) {
	initClient(client)
	client.eventMap[OnMessage] = append(client.eventMap[OnMessage], fun)
}

func (client *TcpClient) OnConnect(fun func(connection net.Conn)) {
	initClient(client)
	client.eventMap[OnConnect] = append(client.eventMap[OnConnect], fun)
}

func (client *TcpClient) OnDisConnect(fun func(connection net.Conn)) {
	initClient(client)
	client.eventMap[OnDisConnect] = append(client.eventMap[OnDisConnect], fun)
}
func (client *TcpClient) OnError(fun func(err error)) {
	initClient(client)
	client.eventMap[OnError] = append(client.eventMap[OnError], fun)
}
func (client *TcpClient) OnClose(fun func(conn net.Conn)) {
	initClient(client)
	client.eventMap[OnClose] = append(client.eventMap[OnClose], fun)
}
func (client *TcpClient) Close() {
	initClient(client)
	client.con.Close()
}
func initClient(client *TcpClient) {
	if client.eventMap == nil {
		client.eventMap = make(map[string][]interface{})
	}
}
func (client *TcpClient) handler(connection net.Conn) {
	initClient(client)
	for true {
		message, err := awesomeTask.ReadString(connection)
		if err != nil {
			if _, ok := client.eventMap[OnClose]; ok {
				closeFuns := client.eventMap[OnClose]
				if client.debug {
					fmt.Println("开启调试模式:", "连接被关闭=>"+connection.RemoteAddr().String())
				}
				for _, closeFun := range closeFuns {
					go closeFun.(func(conn net.Conn))(connection)
				}

			}
			break
		}
		if client.debug {
			fmt.Println("开启调试模式:", "服务端发送数据=>"+connection.RemoteAddr().String()+" msg:"+message)
		}
		if _, ok := client.eventMap[OnMessage]; ok {
			messageFuns := client.eventMap[OnMessage]
			for _, messaheFun := range messageFuns {
				messaheFun = messaheFun
				go messaheFun.(func(connection net.Conn, message string))(connection, message)
			}
		}
	}
}
